package algorithms.leecode;

public class Au {
    public static void main(String[] args) {
        new Au().findNumberIn2DArray(new int[][]{
                {-1},
        {-1}


        },-2);
    }
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        int end = findMid(matrix,target);
        int  start = findMid1(matrix,target);
        System.out.println(end);
        System.out.println(start);
        for(int i = start;i <= end;i++) {
            int[] arr = matrix[i];
            int left = 0;
            int right = arr.length - 1;
            while(left < right) {
                int mid = left + (right - left)/2;
                if(arr[mid] > target) {
                    right = mid - 1;
                } else if(arr[mid] < target) {
                    left = mid + 1;
                } else{
                    return true;
                }
            }
            if(arr[left] == target) {
                return true;
            }
        }
        return false;
    }
    public int findMid(int[][] matrix, int target) {
        int start = 0;
        int end = matrix.length - 1;
        while(start < end) {
            int mid = start + (end - start)/2;
            if(matrix[mid][0] <= target) {
                start = mid + 1;
            } else {
                end = mid - 1;
            }
        }
        return start;
    }
    public int findMid1(int[][] matrix, int target) {
        int start = 0;
        int end = matrix.length - 1;
        while(start < end) {
            int mid = start + (end - start)/2;
            if(matrix[mid][matrix[0].length - 1] >= target) {
                end = mid - 1;
            } else {
                start = mid +1;
            }
        }
        return start;
    }
}
